const cookieparser = process.server ? require('cookieparser') : undefined

export const state = () => ({})

export const actions = {
  nuxtServerInit({ commit }, { ssrContext }) {
    let token = ''
    const { req } = ssrContext
    if (req.headers.cookie) {
      const parsed = cookieparser.parse(req.headers.cookie)
      try {
        // token = JSON.parse(parsed.token)
        token = parsed.token
        // console.log('request', token)
      } catch (err) {
        // No valid cookie found
      }
    }
    commit('user/SET_TOKEN', token)
  },
}
